package com.bflow.web.controller;

import com.bflow.api.IBflowRunInstanceService;
import com.bflow.api.bo.BflowDefineActBo;
import com.bflow.api.bo.BflowDefineActQueryParam;
import com.bflow.api.bo.BflowRunInstanceQueryParam;
import com.bflow.api.bo.ext.BflowUserBo;
import com.bflow.commons.bean.PageResult;
import com.bflow.commons.bean.TableResult;
import com.bflow.util.AssertUtil;
import com.bflow.util.JsonUtil;
import com.bflow.web.util.TestLoginUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 流程申请实例
 */
@Controller
@RequestMapping(value = "/bflow/instance")
public class BflowInstanceController  extends BflowBaseController{

    @Autowired
    private
    IBflowRunInstanceService bflowRunInstanceService;


    @RequestMapping(value = "list")
    @ResponseBody
    public String list(BflowRunInstanceQueryParam queryParam, HttpServletRequest req, HttpServletResponse resp) {
        try {
            BflowUserBo user = TestLoginUtil.getUser(req);
            AssertUtil.isNotNull(user, "您还未登录, 请先登录");
            PageResult pageResult = bflowRunInstanceService.getList(user.getUserId(), null);
            if(CollectionUtils.isEmpty(pageResult.getResult())){
                return JsonUtil.toStr(new TableResult(null,0));
            }
            TableResult result=new TableResult(pageResult.getResult(),pageResult.getTotalCount());
            return JsonUtil.toStr(result);
        } catch (Exception e) {
            return returnFail(e, "列表查询失败");
        }
    }
}
